<?php


/**
* Puree CMS
* http://www.molotsoft.ru
*
* @package Puree_CMS
* @subpackage Controllers
* @author Ivan Chistyakov <ivan@molotsoft.ru>
* @version $Id: contacts_controller.php 833 2010-03-12 08:02:05Z molotsoft@gmail.com $
* @link $HeadURL: http://puree-cms.googlecode.com/svn/trunk/controllers/contacts_controller.php $
*/


class ContactsController extends GeneralController {

/******************************************************************************/

var $name = 'Contacts';

var $components = array('Captcha', 'Email');

/******************************************************************************/

function _sendEmail($to, $from, $subject, $form_data) {
	$error_messages = array();
	// captcha
	if (!isset($form_data['captcha']) || empty($form_data['captcha'])) {
		$error_messages[] = __('empty_captcha_value_message', TRUE);
	} elseif (!$this->Captcha->check() || !$this->Captcha->match($form_data['captcha'])) {
		$error_messages[] = __('invalid_captcha_value_message', TRUE);
	}
	if ($this->Captcha->check()) $this->Captcha->del();
	// name / contacts / message
	if (!isset($form_data['name']) || empty($form_data['name'])) {
		$error_messages[] = __('empty_name_value_message', TRUE);
	}
	if (!isset($form_data['contacts']) || empty($form_data['contacts'])) {
		$error_messages[] = __('empty_contacts_value_message', TRUE);
	}
	if (!isset($form_data['message']) || empty($form_data['message'])) {
		$error_messages[] = __('empty_message_value_message', TRUE);
	}
	// email
	$this->Email->to = $to;
	$this->Email->from = $from;
	$this->Email->subject = $subject;
	$this->Email->template = 'feedback';
	$this->Email->sendAs = 'both';
	if (empty($error_messages) && !$this->Email->send()) {
		$error_messages[] = __('letter_was_not_sent_message', TRUE);
	}
	return $error_messages;
}

function view($id_or_slug = NULL) {
	// parent
	$this->automateRender = FALSE;
	parent::view($id_or_slug);
	extract($this->viewVars, EXTR_SKIP);
	// extends
	if ($this->RequestHandler->isPost()) {
		$this->set('form_data', $form_data = array_filter(array_map('trim', $this->params['form'])));
		$this->set('error_messages', $error_messages = $this->_sendEmail($row[$model]['feedback_to'], $row[$model]['feedback_from'], $row[$model]['feedback_subject'], $form_data));
		// result
		if (empty($error_messages)) {
			$this->_isAjax_JSON(array('email_sent' => TRUE, 'success_message' => __('letter_was_sent_message', TRUE)));
			$this->Session->write('Message.successMessage', __('letter_was_sent_message', TRUE));
			$this->redirect(array('action' => 'view', 'id' => $id));// improve !!!
		} else {
			$this->_isAjax_JSON(array('email_sent' => FALSE, 'error_messages' => $error_messages));
			$this->Session->write('Message.errorMessages', $error_messages);
		}
	} elseif ($this->Captcha->check()) $this->Captcha->del();
	// render
	$this->render();
}

/******************************************************************************/

}


?>